//
//  DiaryWallViewController.m
//  PSYWeiBo
//
//  Created by K.Keyboard on 16/3/15.
//  Copyright © 2016年 郑亚田. All rights reserved.
//

#import "DiaryWallViewController.h"
#import "MainViewController.h"
#import <Masonry/Masonry.h>
#import "DiaryTableViewCell.h"
#import "UrlRequestManager.h"
#import "DiaryModel.h"
#define kUrl @"http://v3.wufazhuce.com:8000/api/hp/more/0"
@interface DiaryWallViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)UIImageView *topImg;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UIButton *backBtn;
@property (nonatomic,strong)NSMutableArray *ARR;

@property (nonatomic,assign) CGFloat cellHeight;

@end

@implementation DiaryWallViewController

-(NSMutableArray *)ARR{
    if (!_ARR) {
        _ARR = [[NSMutableArray alloc] init];
        return _ARR;
    }
    return _ARR;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setViews];
    self.topImg.userInteractionEnabled = YES;
    [self setData];
    self.tableV.delegate =self;
    self.tableV.dataSource = self;

    
    [self.tableV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell_id"];

}
-(void)setData{
    [UrlRequestManager requestWith:GET URL:kUrl condition:nil SuccessBlock:^(id item) {
        NSDictionary *dic = item;
        NSLog(@"%@",dic);
        NSArray *array = [NSArray array];
        array = dic[@"data"];
        for (NSDictionary*d in array) {
            DiaryModel *model = [[DiaryModel alloc] init];
            [model setValuesForKeysWithDictionary:d];
            [self.ARR addObject:model];
            NSLog(@"%@",model.hp_content);
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableV reloadData];
        });
         NSLog(@"%@ ",self.ARR);
    } failBlock:nil];
   
}
-(void)setViews {
    self.topImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dw_header"]];
    [self.tableV addSubview:self.topImg];
    self.topImg.frame = CGRectMake(0, 0, kScreenWidth, 64);
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth*0.45, 10, kScreenWidth*0.1, 60)];
    self.titleLabel.text = @"热门";
    self.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.topImg addSubview:self.titleLabel];
    self.tableV = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    self.tableV.tableHeaderView = self.topImg;
    [self.view addSubview:self.tableV];
    self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [self.topImg addSubview:self.backBtn];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.width.equalTo(@(kScreenWidth * .15));
        make.height.equalTo(@25);
        make.centerY.mas_equalTo(self.titleLabel.mas_centerY);
    }];
    [self.backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];


}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.ARR.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    static NSString *cell_id = @"cell_id";
    DiaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_id" forIndexPath:indexPath];
    if (cell.contentView.subviews.count != 0) {
        [cell.contentView.subviews.firstObject removeFromSuperview];
    }
    DiaryModel *model = self.ARR[indexPath.row];
    CGFloat height = [self cellHeightWithString:model.hp_content];
    UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, kScreenWidth - 20, height)];
    la.numberOfLines = 0;
    la.text = model.hp_content;
    [cell.contentView addSubview:la];
    self.cellHeight = height + 20;
    NSLog(@"%ld",cell.contentView.subviews.count);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.cellHeight;
}

//实现类方法计算高度
-(CGFloat)cellHeightWithString:(NSString *)str{
    
    //限定宽度的参数
    //宽度也一定是跟控件设定的宽度-20
    CGSize size = CGSizeMake(kScreenWidth - 20, MAXFLOAT);
    //设置字体大小
    //这里写的字体大小一定要跟控件中设定的字体大小一样
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    
    //计算一段文本在限定宽度和限定字体大小的情况下占据的矩形框大小
    //attributes 参数必须是字典 所以定义字典接收(dic)
    CGRect rect = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

- (void)backBtnAction{

    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
